package cn.com.travelman.service.push.factory.getui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;

import cn.com.travelman.service.push.bean.PushLinkMessage;
import cn.com.travelman.service.push.bean.PushNotificationMessage;
import cn.com.travelman.service.push.bean.PushTransmissionMessage;

public class PushMessageToList extends PushMessage{
	
	public PushMessageToList(String master,List<String> cidList,String host){
		super.setMaster(master);
		super.setCidList(cidList);
		super.setHost(host);
	}
	//
	private ListMessage initMessage() {
		ListMessage message = new ListMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		// message.setPushNetWorkType(1);
		// //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
		return message;
	}
	//
	private List<Target> initTarget(String appId) {
		List<Target> targets = new ArrayList<Target>();
		for (String cid : this.getCidList()) {
			Target target = new Target();
			target.setAppId(appId);
			target.setClientId(cid);
			// 用户别名推送，cid和用户别名只能2者选其一
			// String alias = "个";
			// target.setAlias(alias);
			targets.add(target);
		}
		return targets;
	}
	//
	public boolean pushTransmission(PushTransmissionMessage content) {
		boolean result = false;
		//
		TransmissionTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		ListMessage message = initMessage();
		message.setData(template);
		//
		List<Target> targets = initTarget(content.getAppId());
		//
        //获取taskID
        String taskId = push.getContentId(message);
        //使用taskID对目标进行推送
        IPushResult ret = push.pushMessageToList(taskId, targets);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;	
	}
	//
	public boolean pushLink(PushLinkMessage content){
		boolean result = false;
		//
		LinkTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		ListMessage message = initMessage();
		message.setData(template);
		//
		List<Target> targets = initTarget(content.getAppId());
		//
        //获取taskID
        String taskId = push.getContentId(message);
        //使用taskID对目标进行推送
        IPushResult ret = push.pushMessageToList(taskId, targets);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;		
	}
	//
	public boolean pushNotification(PushNotificationMessage content){
		boolean result = false;
		//
		NotificationTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		ListMessage message = initMessage();
		message.setData(template);
		//
		List<Target> targets = initTarget(content.getAppId());
		//
        //获取taskID
        String taskId = push.getContentId(message);
        //使用taskID对目标进行推送
        IPushResult ret = push.pushMessageToList(taskId, targets);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;		
	}
}
